package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.SongListMapper;
import com.hc.music.entity.SongList;
import com.hc.music.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class SongListServiceImpl extends ServiceImpl<SongListMapper,SongList> implements SongListService {
    @Autowired
    SongListMapper songListMapper;

    @Override
    public R addSongList(SongList songList) {
        songList.setImage("img/songListPic/user.jpg");
        if(songListMapper.insert(songList) > 0){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @Override
    public R updateSongListMsg(SongList songList) {
        if(songListMapper.updateById(songList) > 0){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @Override
    public R updateSongListImg(MultipartFile file, int id) {
        try {
            deleteSongListPic(id);
            String filename = System.currentTimeMillis() + file.getOriginalFilename();
            String path = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"songListPic" +System.getProperty("file.separator")+ filename;
            File dest = new File(path);
            file.transferTo(dest);
            SongList singer = new SongList();
            singer.setId(id);
            String rp = "img/songListPic/"+ filename;
            singer.setImage(rp);
            if(songListMapper.updateById(singer) > 0){
                return R.success("上传成功",rp);
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    private void deleteSongListPic(Integer id){
        String url = songListMapper.selectById(id).getImage();
        if(!url.contains("user.jpg")){
            String name = url.substring(16);
            String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"songListPic"+System.getProperty("file.separator")+name;
            File fFile = new File(fPath);
            if(fFile.exists()){
                fFile.delete();
            }
        }
    }
    @Override
    public R deleteSongList(Integer id) {
        deleteSongListPic(id);
        if(songListMapper.deleteById(id) > 0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @Override
    public R allSongList() {
        return R.success(null,songListMapper.selectList(null));
    }

    @Override
    public List<SongList> findAllSong() {
        return songListMapper.selectList(null);
    }

    @Override
    public R likeTitle(String title) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper();
        queryWrapper.like("title",title);
        return R.success(null,songListMapper.selectList(queryWrapper));
    }

    @Override
    public R likeStyle(String style) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper();
        queryWrapper.like("style",style);
        return R.success(null,songListMapper.selectList(queryWrapper));
    }
}
